package com.zut.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;


import com.zut.pojo.Admin;
import com.zut.pojo.Parents;
import com.zut.pojo.Product;
import com.zut.pojo.Teacher;
import com.zut.service.AdminService;

import com.zut.service.impl.ParentsServiceImpl;
import com.zut.service.impl.ProductServiceImpl;
import com.zut.service.impl.TeacherServiceImpl;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;

import java.util.List;

/**
 *
 *  后台管理员控制器
 *
 */
@Controller
public class AdminController {

    @Resource
    private AdminService adminService;

    @Resource
    private TeacherServiceImpl teacherService;

    @Resource
    private ParentsServiceImpl parentsService;

    @Resource
    private ProductServiceImpl productService;

    //管理员登录
    @RequestMapping("/admlogin")
    public String index( String name, String password, Model model){
        QueryWrapper<Admin> wrapper=new QueryWrapper<>();
        wrapper.eq("username",name).eq("userpwd",password);
        Admin admin=adminService.getOne(wrapper);
        if (admin!=null){
            // 小问题
            return "back/index";
        }else {
            model.addAttribute("msg","账号或密码错误，请重新输入");
            return "back/login";
        }
    }

    //管理员界面展示所有家长信息
    @RequestMapping("/admpar")
    public String selectAllParents(Model model){
        List<Parents> parents = parentsService.selectAllParents();
        model.addAttribute("pars",parents);
        return "back/students";
    };
    //模糊查询家长信息
    @RequestMapping("/admpar/searchpar")
    public String searchpar(@RequestParam("seapar") String seapar, Model model){
        List<Parents> parents = parentsService.seapar(seapar);
        model.addAttribute("pars",parents);
        return "back/students";
    };
    //管理员界面展示所有老师
    @RequestMapping("/admtea")
    public String admtea(Model model){
        List<Teacher> teachers = teacherService.selectAllTeacher();
        model.addAttribute("teas", teachers);
        return "back/teachers";
    }

    //管理员展示教师详情
    @RequestMapping("/admteadetail/{id}")
    public String admteadetail(@PathVariable("id") Integer id, Model model){
        if(id==null) id=200001;
        Teacher teacher=teacherService.selectTeacherById(id);
        model.addAttribute("teadetail",teacher);
       return "back/teacher-details";
    }


    @GetMapping("/toaddtea")
    public String toaddtea(){
        return "back/add-teacher";
    }
    //管理员添加老师,返回true添加成功
    @PostMapping("/toaddtea")
    public String addTeacher(Teacher teacher,Model model){
        QueryWrapper<Teacher> wrapper=new QueryWrapper<>();
        wrapper.eq("teaname",teacher.getTeaname());
        Teacher teacher1=teacherService.getOne(wrapper);
        if(teacher1!=null){
            model.addAttribute("msg","添加失败，用户名已被使用");
            return "back/add-teacher";
        }
        boolean i = teacherService.addTeacher(teacher);
        if(i==true){
            model.addAttribute("msg","添加成功");
        }else{
            model.addAttribute("msg","添加失败");
        }
        return "back/add-teacher";
    }

    //模糊查询老师信息
    @RequestMapping("/admtea/searchtea")
    public String searchtea(@RequestParam("seatea") String seatea, Model model){
        List<Teacher> teachers = teacherService.seatea(seatea);
        model.addAttribute("teas", teachers);
        return "back/teachers";
    };

    //修改老师信息
    @GetMapping("/updateTeacher/{id}")
    public String updateTea(@PathVariable("id") Integer id,Model model){
        Teacher teacher = teacherService.selectTeacherById(id);
        model.addAttribute("tea",teacher);
        return "back/edit-teacher";
    }


    @PostMapping("/updateTeacher/{id}")
    public String updateTeacher(Teacher teacher,Model model){
        boolean flag= teacherService.updateTeacher(teacher);
        System.out.println(flag);
        if(flag!=false){
            model.addAttribute("msg","修改成功");
        }else{
            model.addAttribute("msg","修改失败");
        }
        int teaid=teacher.getTeaid();
        String s="redirect:/updateTeacher/"+teaid;
        return s;
    }
// 删除老师
    @GetMapping("/admdeltea/{id}")
    public String admdeltea(@PathVariable("id") Integer id,Model model){
        teacherService.deleteTeacher(id);
        return "redirect:/admtea";
    }
    //管理⚪展示课程包界面
    @RequestMapping("/admpro")
    public  String admpro(Model model){
        List<Product> products =productService.list();
        model.addAttribute("pros",products);
        return "back/subjects";
    }
    //管理员模糊查询课程信息
    @RequestMapping("/admpro/searchpro")
    public String searchpro(@RequestParam("seapro") String seapro, Model model){
        List<Product> productList = productService.seapro(seapro);
        model.addAttribute("pros", productList);
        return "back/subjects";
    };


    //404界面
    @RequestMapping("/admerror")
    public String admerror(){
        return "back/error-404";
    }
    //空白页
    @RequestMapping("/admpage")
    public String admpage(){
        return "back/blank-page";
    }

}

